package org.jackysoft.interceptor;

import java.lang.reflect.Method;

import org.apache.commons.beanutils.PropertyUtils;
import org.jackysoft.query.Query;

import com.opensymphony.xwork2.ActionInvocation;

public class QueryInterceptor extends AbstractInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7959137668639709900L;
	
	@Override
	public String intercept(ActionInvocation ai) throws Exception {
		super.intercept(ai);
		Object action = ai.getAction();
		Integer page = (Integer) parseBean(action,"page");
		Integer rows = (Integer) parseBean(action,"rows");
	    if(page==null)page=0;
	    if(rows==null)rows=10;
		Query q = (Query) parseBean(action,"query");
		Method service_m_gets = service.getClass().getMethod("query",Query.class,Integer.class,Integer.class);
		// 从数据库查询得到的对象
		Object pageBean = service_m_gets.invoke(service,q,page,rows);		
		PropertyUtils.setProperty(pageBean,"actionClass", action.getClass());
		PropertyUtils.setProperty(pageBean, "method", ai.getProxy().getMethod());
		PropertyUtils.setProperty(action, "pager", pageBean);
		return ai.invoke();
	}

}
